package cn.lds.j375.healthexam.service.impl;

import cn.lds.j375.healthexam.condition.AppointmentRecordCondition;
import cn.lds.j375.healthexam.dto.ResultDTO;
import cn.lds.j375.healthexam.entity.AppointmentRecord;
import cn.lds.j375.healthexam.entity.ExamRecord;
import cn.lds.j375.healthexam.entity.HealthExamPackage;
import cn.lds.j375.healthexam.entity.User;
import cn.lds.j375.healthexam.exception.SysException;
import cn.lds.j375.healthexam.mapper.AppointmentRecordMapper;
import cn.lds.j375.healthexam.mapper.ExamRecordMapper;
import cn.lds.j375.healthexam.mapper.HealthExamPackageMapper;
import cn.lds.j375.healthexam.mapper.UserMapper;
import cn.lds.j375.healthexam.service.AppointmentRecordService;
import cn.lds.j375.healthexam.service.ExamRecordService;
import cn.lds.j375.healthexam.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Service
public class AppointMentRecordServiceImpl implements AppointmentRecordService {

    @Autowired
    private AppointmentRecordMapper appointmentRecordMapper;

    private static final Logger log = LoggerFactory.getLogger(AppointMentRecordServiceImpl.class);

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HealthExamPackageMapper healthExamPackageMapper;

    @Autowired
    private ExamRecordMapper examRecordMapper;


    @Override
    public ResultDTO<List<AppointmentRecord>> selectAppointMentRecordAll(AppointmentRecordCondition condition) throws SysException {

        List<AppointmentRecord> appointmentRecords = appointmentRecordMapper.selectAppointMentRecordAll(condition);

        if (CollectionUtils.isEmpty(appointmentRecords)){
            throw new SysException(ResultDTO.buildError("No"));
        }

        return ResultDTO.buildSuccess(appointmentRecords);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO<AppointmentRecord> selectAppointMentRecordById(Integer id) throws SysException {


        AppointmentRecord appointmentRecord = appointmentRecordMapper.selectAppointmentRecordById((id));

        log.info("log:{}",appointmentRecord);

        if (Objects.isNull(appointmentRecord)){
            throw new SysException(ResultDTO.buildError("数据没有被查询到"));
        }

        return ResultDTO.buildSuccess(appointmentRecord);
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO addAppointmentRecord(AppointmentRecord appointmentRecord) throws SysException {

        //获取要预约的人的id
        Long userId = appointmentRecord.getUserId();
        //获取预约人信息
        User user = userMapper.selectById(userId);
        //获取预约套餐
        HealthExamPackage healthExamPackage = healthExamPackageMapper.selectById(appointmentRecord.getExamPackageId());


        if (user.getAge() > healthExamPackage.getMaxAge() || user.getAge() < healthExamPackage.getMinAge()){
            throw new SysException(ResultDTO.buildError("年龄不符合"));
        }else if (healthExamPackage.getGender() != 2 && user.getGender() != healthExamPackage.getGender()){
            throw new SysException(ResultDTO.buildError("性别不符合"));
        }


        int insert = appointmentRecordMapper.insert(appointmentRecord);


        return ResultDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO comfirmAppointmentRecord(Long appointmentId) {

        AppointmentRecord appointmentRecord = new AppointmentRecord();

        appointmentRecord.setId(appointmentId);

        appointmentRecord.setAppointmentStatus(2);
        //确认预约
        appointmentRecordMapper.updateById(appointmentRecord);


        return ResultDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public ResultDTO cancelAppointmentRecord(AppointmentRecord appointmentRecord) {
        appointmentRecord.setAppointmentStatus(0);

        int i = appointmentRecordMapper.updateById(appointmentRecord);

        return ResultDTO.buildSuccess();
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public ResultDTO removeAppointmentRecord(Integer id) {
        return null;
    }
}
